import { request } from '@/api/service'
import { urlPrefix as userPrefix } from '@/views/system/user/api'

export const crudOptions = (vm) => {
  return {
    pageOptions: {
      compact: true
    },
    options: {
      height: '100%'
    },
    rowHandle: {
      width: 140,
      view: {
        thin: true,
        text: '',
        disabled () {
          return !vm.hasPermissions('Retrieve')
        }
      },
      edit: {
        thin: true,
        text: '',
        disabled () {
          return !vm.hasPermissions('Update')
        }
      },
      remove: {
        thin: true,
        text: '',
        disabled () {
          return !vm.hasPermissions('Delete')
        }
      }
    },
    viewOptions: {
      componentType: 'form'
    },
    formOptions: {
      defaultSpan: 24, // 默认的表单 span
      width: '35%'
    },
    indexRow: { // 或者直接传true,不显示title，不居中
      title: '序号',
      align: 'center',
      width: 100
    },
    columns: [
      {
        title: '关键词',
        key: 'search',
        show: false,
        disabled: true,
        search: {
          disabled: false
        },
        form: {
          disabled: true,
          component: {
            placeholder: '请输入关键词'
          }
        },
        view: {
          disabled: true
        }
      },
      {
        title: 'ID',
        key: 'id',
        width: 90,
        disabled: true,
        form: {
          disabled: true
        }
      },
      {
        title: '工单名称',
        key: 'name',
        search: {
          disabled: true
        },
        width: 80,
        type: 'input',
        form: {
          rules: [ // 表单校验规则
            { required: true, message: '请选择一个选项' }
          ],
          component: {
            placeholder: '请输入工单名称'
          },
          itemProps: {
            class: { yxtInput: false }
          }
        }
      },
      {
        title: '审批人',
        key: 'approver',
        search: {
          disabled: false
        },
        disabled: false,
        width: 80,
        type: 'select',
        dict: {
          url: userPrefix + '?querytype=approver',
          label: 'name',
          value: 'id',
          getData: (url, dict) => {
            return request({ url: url }).then(ret => {
              return ret.data
            })
          }
        },
        form: {
          rules: [ // 表单校验规则
            { required: true, message: '审批人' }
          ]
        },
        component: {
          props: { color: 'auto' }
        } // 自动染色
      },
      {
        title: '处理人',
        key: 'executor',
        search: {
          disabled: false
        },
        disabled: false,
        width: 80,
        type: 'select',
        dict: {
          url: userPrefix + '?querytype=executor',
          label: 'name',
          value: 'id',
          getData: (url, dict) => {
            return request({ url: url }).then(ret => {
              return ret.data
            })
          }
        },
        form: {
          rules: [ // 表单校验规则
            { required: true, message: '处理人' }
          ]
        },
        component: {
          props: { color: 'auto' }
        } // 自动染色
      }
    ].concat(vm.commonEndColumns({ show_create_datetime: false })),
    formGroup: {
      type: 'collapse', // tab
      accordion: false,
      groups: {
        base: {
          title: '基本设置',
          icon: 'el-icon-goods',
          columns: ['name', 'approver', 'executor']
        },
        info: {
          title: '详情',
          collapsed: true,
          icon: 'el-icon-warning-outline',
          columns: ['description', 'dept_belong_id']
        }
      }
    }
  }
}
